package com.ddx.service.impl;

import com.ddx.common.exception.ResultModel;
import com.ddx.dao.AdvertMapper;
import com.ddx.pojo.Advert;
import com.ddx.pojo.AdvertVideo;
import com.ddx.pojo.ShoppingProduct;
import com.ddx.service.AdvertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: liyuxiang
 * @create: 2019-01-26
 */
@Service
public class AdvertServiceImpl implements AdvertService {

    @Autowired
    AdvertMapper advertMapper;

    @Override
    public ResultModel getAdvertListByUserId(int userId) {
        List<Advert> advertList = advertMapper.selectByUserId(userId);
        ResultModel resultModel = ResultModel.getSuccessInstance();
        resultModel.setData(advertList);
        return resultModel;
    }

    @Override
    public ResultModel getAdvertById(int advertId) {
        Advert advert = advertMapper.selectByPrimaryKey(advertId);
        ResultModel resultModel = ResultModel.getSuccessInstance();
        resultModel.setData(advert);
        return resultModel;
    }

    @Override
    public Advert getAdvertByVideoId(int videoId) {
        return advertMapper.selectAdvertByVideoId(videoId);
    }

    @Override
    public Advert insertByProduct(ShoppingProduct product, int userId) {
        Advert advert = new Advert();
        advert.setImageUrl(product.getPicture());
        advert.setName(product.getTitle());
        advert.setType("product");
        advert.setDailyPrice(product.getNowPrice());
        advert.setPrice(product.getPrice());
        advert.setProductId(product.getId());
        advert.setUserId(userId);
        advertMapper.insertSelective(advert);
        return advert;
    }
}
